package sostats.core.base;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;

import sostats.core.metadata.Describable;

public class Table implements Describable{
	private final String name;
	private String pk;
	private final List<Attribute> categories;
	private final List<Attribute> summaries;
	private final Map<Joint,Table> children;
	private final List<Attribute> conditions;
	public Table(String name){
		this.name = name;
		this.categories = new ArrayList<Attribute>();
		this.summaries = new ArrayList<Attribute>();
		this.conditions = new ArrayList<Attribute>();
		this.children = new HashMap<Joint, Table>();
	}
	public String getName(){
		return name;
	}
	public void setPK(String pk){
		this.pk = pk;
	}
	public Table addCategory(String name){
		categories.add(new Attribute(name));
		return this;
	}
	public Table addSummary(String name){
		summaries.add(new Attribute(name));
		return this;
	}
	public Table addJointTable(String fk,Table table, String pk){
		Joint joint = new Joint(fk,pk);
		children.put(joint, table);
		return this;
	}
	public List<Attribute> getSummaries() {
		return new ArrayList<Attribute>(summaries);
	}
	public List<Attribute> getCategories() {
		return new ArrayList<Attribute>(categories);
	}
	public List<Joint> getJoint(String table2) {
		ArrayList<Joint> joints = new ArrayList<Joint>();
		for(Entry<Joint,Table> entry:children.entrySet()){
			if(entry.getValue().getName().equals(table2)){
				joints.add(entry.getKey());
			}
		}
		return joints;
	}
	public Table addCondtion(String name) {
		conditions.add(new Attribute(name));
		return this;
	}
	public List<Attribute> getConditions() {
		return new ArrayList<Attribute>(conditions);
	}
	public String getPK() {
		return pk;
	}
}
